package com.jajahome.pop;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.jajahome.R;
import com.jajahome.event.EventMessage;
import com.jajahome.feature.diy.DiyDetailAct;

import org.greenrobot.eventbus.EventBus;

public class PopCamera {
    private Context mContext;
    private View view;
    private PopupWindow mPopupWindow;
    private Button btn_camera,btn_photo,btn_close;
    private LinearLayout pop_ll;
    public PopCamera(Context context,View view){
        this.mContext = context;
        this.view = view;
        initView();
    }
    private void initView(){
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_camera, null);
        btn_camera =(Button) rootView.findViewById(R.id.btn_camera);
        btn_photo = (Button)rootView.findViewById(R.id.btn_photo);
        btn_close = (Button)rootView.findViewById(R.id.btn_close);
        pop_ll = (LinearLayout)rootView.findViewById(R.id.pop_ll);
        pop_ll.getBackground().setAlpha(80);
        mPopupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventMessage(DiyDetailAct.DIY_REQUEST_CAMERA_MSG, "camera"));
                dismiss();
            }
        });
        btn_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventMessage(DiyDetailAct.DIY_REQUEST_PHOTO,"photo"));
                dismiss();
            }
        });
    }

    /**
     * 显示popupwindow
     */
    public void show() {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 关闭popupwindow
     */
    public void dismiss() {
        if (mPopupWindow.isShowing()) mPopupWindow.dismiss();
    }

    protected void showToast(String str) {
        //去换行
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
